<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style></style>
  </head>
  <body>
    <div>
      <h1>For循环优化</h1>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
      <p class="btn"></p>
    </div>
    <script>
      var aBtns = document.getElementsByClassName("btn");
      for (let i = 0; i < aBtns.length; i++) {
        console.log(i);
      }
      //缓存aBtns长度,就不需要向上面的for一样,每次的获取length
      for (let i = 0, len = aBtns.length; i < len; i++) {
        console.log(i);
      }
    </script>
  </body>
</html>
